﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Devfw.OpenAuth;
using System.Net;
using Devfw;

namespace Devfw.OpenAuth.Platform
{
    public class Tencent : ProviderBase
    {
        public Tencent( string oauth_consumer_key, string oauth_consumer_secret )
            : base( oauth_consumer_key, oauth_consumer_secret )
        {
            _core = new Devfw.OpenAuth.OpenAuthCore( oauth_consumer_key, oauth_consumer_secret, "1.0", SignatureType.HMACSHA1, AuthorizationType.UrlQuery );
        }

        public Tencent( string oauth_consumer_key, string oauth_consumer_secret, string oauth_token, string oauth_token_secret )
            : base( oauth_consumer_key, oauth_consumer_secret, oauth_token, oauth_token_secret )
        {
            _core = new Devfw.OpenAuth.OpenAuthCore( oauth_consumer_key, oauth_consumer_secret, "1.0", SignatureType.HMACSHA1, AuthorizationType.UrlQuery );
        }

        #region OAuth
        public RequestTokenResponse request_token( string oauth_callback )
        {
            return _core.request_token( "https://open.t.qq.com/cgi-bin/request_token", oauth_callback );
        }

        public string GetAuthorizeUrl(string requestToken, string oauth_callback)
        {
            if(!string.IsNullOrEmpty(requestToken) )
                return _core.generate_authorize_url("https://open.t.qq.com/cgi-bin/authorize", requestToken, oauth_callback);
            
            return string.Empty;
        }

        
        public AccessTokenResponse access_token( string oauth_token, string oauth_token_screct, string oauth_verifier )
        {
            return _core.access_token( "https://open.t.qq.com/cgi-bin/access_token", oauth_token, oauth_token_screct, oauth_verifier );
        }
        #endregion

        public string t_get(string format, string blogID)
        {
            string response = _core.OpenAuthWebRequest(HttpWebMethod.POST, "http://open.t.qq.com/api/t/show", OAuthToken, OAuthTokenSecret,
               new ParameterCollection()
                {
                    new Parameter("format", format, ParameterType.Get),
                    new Parameter("id", blogID, ParameterType.Get)
                });
            return response;
        }

        public string t_add( string format, string content, string clienIP )
        {
            // _core.AuthorizationType = AuthorizationType.PostRequestBody;
            string response = _core.OpenAuthWebRequest( HttpWebMethod.POST, "http://open.t.qq.com/api/t/add", OAuthToken, OAuthTokenSecret,
                new ParameterCollection()
                {
                    new Parameter("format", format, ParameterType.Post),
                    new Parameter("content", content, ParameterType.Post),
                    new Parameter("clientip", HttpUtility.UrlEncode( clienIP ), ParameterType.Post),
                });
            return response;
        }

        public string t_add_pic( string format, string content, string clienIP, byte[] imageByte, string fileName )
        {
            // _core.AuthorizationType = AuthorizationType.PostRequestBody;
            var contentEncoding = "iso-8859-1";
            string fileData = System.Text.Encoding.GetEncoding( contentEncoding ).GetString( imageByte );
            // string fileData = System.Text.Encoding.ASCII.GetString( imageByte );

            _core.AuthorizationType = AuthorizationType.FormDataPost;
            string response = _core.OpenAuthFormDataPostWebRequest( "http://open.t.qq.com/api/t/add_pic", OAuthToken, OAuthTokenSecret,
                new ParameterCollection()
                {
                    new PostParameter("format", format ),
                    new PostParameter("content", content),
                    new PostParameter("jing", ""),
                    new PostParameter("wei", ""),
                    new PostParameter("clientip", clienIP),
                    // new PostParameter( "pic", fileData, "utf-8", "application/octet-stream", "binary", "filename=\"" + fileName + "\"", false )
                    new PostParameter( "pic", fileData, true, "filename=\"" + fileName + "\"", false )
                } );
            return response;
        }

        public string t_add_pic( string format, string content, string clienIP, string url )
        {
            WebClient client = new WebClient();
            byte[] b = client.DownloadData( url );
            string[] f = url.Split( '\\', '/' );
            return t_add_pic( format, content, clienIP, b, f[f.Length - 1] );
        }

        public string get_other_info(string fopenID,string name)
        {
            string response = _core.OpenAuthWebRequest(HttpWebMethod.POST, "http://open.t.qq.com/api/user/other_info" , OAuthToken, OAuthTokenSecret,
                new ParameterCollection()
                {
                    
                    new Parameter( "format", "xml", ParameterType.Get ),
                    new Parameter( "name", name , ParameterType.Get ),
                    new Parameter( "fopenid", fopenID , ParameterType.Get )
                    
                });

            return response;
        }

        public string StatusesRepost(string content, string id, string clientip)
        {
            return StatusesRepost(content, id, clientip,"","");
        }
        public string StatusesRepost(string content, string id, string clientip, string jing, string wei)
        {
            string response = _core.OpenAuthWebRequest(HttpWebMethod.POST, "http://open.t.qq.com/api/t/re_add", OAuthToken, OAuthTokenSecret,
                new ParameterCollection()
                {
                    
                    new Parameter( "format", "xml", ParameterType.Post ),
                    new Parameter( "content", content , ParameterType.Post ),
                    new Parameter( "clientip", HttpUtility.UrlEncode(clientip) , ParameterType.Post ),
                    new Parameter( "jing", jing , ParameterType.Post ),
                    new Parameter( "wei", wei , ParameterType.Post ),
                    new Parameter( "reid", id , ParameterType.Post )
                });

            return response;
        }


        public string StatusesComment(string content, string id, string clientip)
        {
            return StatusesComment(content, id, clientip, "", "");
        }

        public string StatusesComment(string content, string id, string clientip, string jing, string wei)
        {
            string response = _core.OpenAuthWebRequest(HttpWebMethod.POST, "http://open.t.qq.com/api/t/comment", OAuthToken, OAuthTokenSecret,
                new ParameterCollection()
                {
                    new Parameter( "format", "xml", ParameterType.Post ),
                    new Parameter( "content", content , ParameterType.Post ),
                    new Parameter( "Clientip", HttpUtility.UrlEncode(clientip) , ParameterType.Post ),
                    new Parameter( "jing", jing , ParameterType.Post ),
                    new Parameter( "wei", wei , ParameterType.Post ),
                    new Parameter( "reid", id , ParameterType.Post )
                });

            return response;

        }

        public string getFanslist(string reqnum, string startindex)
        {
            return getFriendlist( "http://open.t.qq.com/api/friends/fanslist", reqnum, startindex);
        }

        public string getIdollist(string reqnum, string startindex)
        {
            return getFriendlist("http://open.t.qq.com/api/friends/idollist", reqnum, startindex);
        }

        public string getFriendlist(string url, string reqnum, string startindex)
        {
            string response = _core.OpenAuthWebRequest(HttpWebMethod.GET, url, OAuthToken, OAuthTokenSecret,
                new ParameterCollection()
                {
                    
                    new Parameter( "format", "xml", ParameterType.Get ),
                    new Parameter( "reqnum", reqnum , ParameterType.Get ),
                    new Parameter( "startindex", startindex , ParameterType.Get )
                    
                });

            return response;
        }

        public string getUserShow()
        {
            string response = _core.OpenAuthWebRequest(HttpWebMethod.POST, "http://open.t.qq.com/api/user/info", OAuthToken, OAuthTokenSecret,
                new ParameterCollection()
                {
                    
                    new Parameter( "format", "xml", ParameterType.Get )
                    
                });

            return response;
        }

        public string setSetFriendship(string acttype, string id)
        {

            string response = _core.OpenAuthWebRequest(HttpWebMethod.POST, string.Format("http://open.t.qq.com/api/friends/{0}", acttype), OAuthToken, OAuthTokenSecret,
                new ParameterCollection()
                {
                    new Parameter( "format", "xml", ParameterType.Post ),
                    new Parameter( "name", id, ParameterType.Post )
                });

            return response;
        }

        public string addFriends(string[] fopenids)
        {
            string response = _core.OpenAuthWebRequest(HttpWebMethod.POST, "http://open.t.qq.com/api/friends/add", OAuthToken, OAuthTokenSecret,
                new ParameterCollection()
                {
                    
                    new Parameter( "format", "xml", ParameterType.Post ),
                    new Parameter( "fopenids",string.Join( "_", fopenids ) , ParameterType.Post )
                    
                });

            return response;
        }

        public string addFriend(string fopenid)
        {
            return addFriends(new string[]{fopenid});
        }

        public string removeFriend(string fopenid)
        {
            string response = _core.OpenAuthWebRequest(HttpWebMethod.POST, "http://open.t.qq.com/api/friends/del", OAuthToken, OAuthTokenSecret,
                new ParameterCollection()
                {
                    
                    new Parameter( "format", "xml", ParameterType.Post ),
                    new Parameter( "fopenid",fopenid , ParameterType.Post )
                    
                });

            return response;
        }
    }
}
